package org.example.book.bridge;

import org.example.book.pojo.UserInfo;
import org.example.book.repo.UserRepository;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author jeff
 */
public abstract class AbstractRegisterLoginFunc implements RegisterLoginFuncInterface {

    protected String commonLogin(String account, String password, UserRepository userRepository) {
        UserInfo userInfo = userRepository.findByUserNameAndUserPassword(account, password);
        if (userInfo == null) {
            return "account / password incorrect";
        }
        return "login success";
    }

    protected String commonRegister(UserInfo userInfo, UserRepository userRepository) {
        if (checkExists(userInfo.getUserName(), userRepository)) {
            return "account / username already in use";
        }
        userInfo.setCreateDate(new Date());
        userRepository.save(userInfo);
        return "register success";
    }

    protected boolean checkExists(String account, UserRepository userRepository) {
        UserInfo userInfo = userRepository.findByUserName(account);
        return userInfo != null;
    }

    @Override
    public String login(String username, String password) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String register(UserInfo userInfo) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean checkExists(String username) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String login3rd(HttpServletRequest request) {
        throw new UnsupportedOperationException();
    }
}
